<template>
<div>
    <!--搜索栏-->
    <!-- <el-form :inline="true" :model="formInline" class="user-search">
        <el-form-item label="搜索：">
            <el-input size="small" v-model="formInline.userName" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button size="small" type="primary" icon="el-icon-search" @click="getData">搜索</el-button>
        </el-form-item>
        <el-form-item>
            <el-button size="small" type="success" icon="el-icon-search" @click="handleAdd">添加</el-button>
        </el-form-item>
    </el-form> -->

    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
        <el-table-column sortable prop="userName" label="姓名"></el-table-column>
        <el-table-column sortable prop="userType" :formatter="formatUserType" label="用户身份"></el-table-column>
        <el-table-column sortable prop="foodName" label="菜品名称"></el-table-column>
        <el-table-column sortable prop="price" label="单价"></el-table-column>
        <el-table-column sortable prop="amount" label="数量"></el-table-column>
        <el-table-column sortable prop="totalPrice" label="总价"></el-table-column>
        <el-table-column sortable prop="percentMoney" label="提成"></el-table-column>
        <el-table-column sortable prop="createTime" label="时间"></el-table-column>
        <el-table-column align="center" label="操作">
            <template slot-scope="scope">
                <!-- <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button> -->
                <el-button size="mini" type="danger" @click="handleDel(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>

    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="50%" @click="closeDialog">
        <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
            <el-form-item label="姓名" prop="userName">
                <el-input v-model="editForm.userName"></el-input>
            </el-form-item>
            <el-form-item label="账号" prop="userAccount">
                <el-input v-model="editForm.userAccount"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="userPassword">
                <el-input v-model="editForm.userPassword" type="password"></el-input>
            </el-form-item>
            <el-form-item label="用户类型" prop="userPassword">
                <el-select v-model="editForm.userType" placeholder="请选择用户类型">
                    <el-option v-for="item in userTypes" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
                <el-radio-group v-model="editForm.sex">
                    <el-radio class="radio" :label="'1'">男</el-radio>
                    <el-radio class="radio" :label="'0'">女</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="年龄" prop="age">
                <el-input-number v-model="editForm.age" :min="0" :max="100"></el-input-number>
            </el-form-item>
            <el-form-item label="电话号码" prop="phoneNumber">
                <el-input v-model="editForm.phoneNumber"></el-input>
            </el-form-item>
            <el-form-item label="工资" prop="salary">
                <el-input v-model="editForm.salary"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button size="small" @click="closeDialog">取消</el-button>
            <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">提交</el-button>
        </div>
    </el-dialog>
</div>
</template>

<script>
import * as api from '../../api/interface'
import Pagination from '../../components/Pagination'
export default {
    data() {
        return {
            loading: false, //是显示加载
            editFormVisible: false, //控制编辑页面显示与隐藏
            title: '',
            editForm: {},
            // rules表单验证
            rules: {
                userName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
                userAccount: [{ required: true, message: '请输入账号', trigger: 'blur' }],
                userPassword: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                userType: [{ required: true, message: '请输入用户类型', trigger: 'blur' }],
                phoneNumber: [{ required: true, message: '请输入电话号码', trigger: 'blur' }]
            },
            //查询条件
            formInline: {
                page: 1, //当前页面
                limit: 10, //每页数量
                userName: ''
            },
            // 删除部门
            seletedata: {
                ids: '',
                token: localStorage.getItem('logintoken')
            },
            userparm: [], //搜索权限
            listData: [], //用户数据
            // 分页参数
            pageparm: {
                currentPage: 1,
                pageSize: 10,
                total: 10
            },
            userTypes: [
                { value: "1", label: "点餐员" },
                { value: "2", label: "配餐员" },
                { value: "3", label: "仓库管理员" }
            ],
        }
    },
    // 注册组件
    components: {
        Pagination
    },
    /**
     * 页面初始化方法
     */
    created() {
        console.log(this.$store.state.userInfo)
        this.getData()
    },
    methods: {
        formatUserType: function (row, column) {
            return row.userType == '0' ? "管理员" :
                row.userType == "1" ? "点餐员" :
                row.userType == "2" ? "配餐员" :
                row.userType == "3" ? "仓库管理员" :
                "未知";
        },
        formatSex: function (row, column) {
            return row.sex == '0' ? "女" :
                row.sex == "1" ? "男" :
                "未知";
        },
        // 获取列表数据
        getData() {
            this.loading = true
            let para = {
                current: this.formInline.page,
                size: this.formInline.limit,
                userName: this.formInline.userName
            }
            api.querySaleLogByPage(para).then(data => {
                this.logining = false;
                if (data.message != "success") {
                    this.logining = false;
                    this.$message.error(data.message)
                } else {
                    this.loading = false
                    this.listData = data.returnPage.records
                    this.pageparm.currentPage = data.returnPage.current
                    this.pageparm.pageSize = data.returnPage.size
                    this.pageparm.total = data.returnPage.total
                }
            }).catch(e => {
                this.logining = false;
            });
        },
        // 分页插件事件
        callFather(parm) {
            this.formInline.page = parm.currentPage
            this.formInline.limit = parm.pageSize
            this.getData()
        },
        //显示添加界面
        handleAdd: function (index, row) {
            this.editFormVisible = true
            this.title = '新增'
            this.editForm = {}
        },
        //显示编辑界面
        handleEdit: function (index, row) {
            this.editFormVisible = true
            this.title = '编辑'
            this.editForm = row
        },
        // 关闭编辑
        closeDialog() {
            this.editFormVisible = false
        },
        // 编辑、增加页面保存方法
        submitForm(editData) {
            this.$refs[editData].validate(valid => {
                if (valid) {
                    let para = Object.assign({}, this.editForm);
                    this.loading = true;
                    api.updateSaleLog(para)
                        .then(res => {
                            this.loading = false;
                            if (res.message == 'success') {
                                this.$message({ message: "提交成功", type: "success" });
                                this.getData();
                                this.editFormVisible = false;
                            } else {
                                this.$message({ type: 'error', message: res.message })
                            }
                        })
                        .catch(e => {
                            // this.editFormVisible = false
                            this.loading = false
                        })
                } else {
                    return false
                }
            })
        },
        // 删除
        handleDel(index, row) {
            this.$confirm('确定要删除吗?', '信息', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' })
                .then(() => {
                    let para = {
                        saleLogId: row.saleLogId
                    }
                    api.delSaleLog(para)
                        .then(res => {
                            if (res.message == 'success') {
                                this.$message({ message: "删除成功", type: "success" });
                                this.getData();
                            } else {
                                this.$message({ type: 'error', message: res.message })
                            }
                        })
                        .catch(e => {
                            this.loading = false
                        })
                });
        }
    }
}
</script>

<style scoped>
.user-search {
    margin-top: 20px;
}
</style>
